/**
 * 
 */
package ru.olympus.antonyhands.ao.client.widgets;

import ru.olympus.antonyhands.ao.client.AOHelperServiceAsync;
import ru.olympus.antonyhands.ao.shared.item.ItemDTO;
import ru.olympus.antonyhands.ao.shared.util.StringUtilities;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * @author AntonyHands
 * @since 28.02.2012
 */
public class ItemInfoGrid extends Grid
{
    private AOHelperServiceAsync _service;
    private ItemsListBox _itemsListBox;

    private VerticalPanel mainInfoPanel;
    private Label title;
    private Label category;
    private Label fixedPrice;
    private Label craftPrice;
    private TextBox marketPrice;
    private Button changeMarketPriceButton;
    private ItemCraftTable2 craftTable;
    private FlexTable craftedItemsTable;

    public ItemInfoGrid(AOHelperServiceAsync service, ItemsListBox itemsListBox)
    {
        super(2,2);
        _service = service;
        _itemsListBox = itemsListBox;
        
        mainInfoPanel = new VerticalPanel();
        setWidget(0, 0, mainInfoPanel);
        
        mainInfoPanel.add(new Label("Основная информация"));
        
        Grid mainInfoGrid = new Grid(5, 2);
        mainInfoPanel.add(mainInfoGrid);

        title = new Label();
        mainInfoGrid.setHTML(0, 0, "Название");
        mainInfoGrid.setWidget(0, 1, title);
        
        category = new Label();
        mainInfoGrid.setHTML(1, 0, "Категория");
        mainInfoGrid.setWidget(1, 1, category);

        fixedPrice = new Label();
        mainInfoGrid.setHTML(2, 0, "Фиксированная цена");
        mainInfoGrid.setWidget(2, 1, fixedPrice);

        craftPrice = new Label();
        mainInfoGrid.setHTML(3, 0, "Крафтовая цена");
        mainInfoGrid.setWidget(3, 1, craftPrice);

        mainInfoGrid.setHTML(4, 0, "Рыночная цена");
        HorizontalPanel marketPricePanel = new HorizontalPanel();
        mainInfoGrid.setWidget(4, 1, marketPricePanel);
        
        marketPrice = new TextBox();
        marketPricePanel.add(marketPrice);
        
        changeMarketPriceButton = new Button("Изменить");
        changeMarketPriceButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				_service.saveMarketPrice(_itemsListBox.getCurrentValueCode(), marketPrice.getText(), 
						new AsyncCallback<String>() {

							@Override
							public void onFailure(Throwable caught) {
								// TODO Auto-generated method stub
								 title.setText("Ошибка работы с сервисом");
							}

							@Override
							public void onSuccess(String result) {
								// TODO Auto-generated method stub
								if (!StringUtilities.isEmpty(result))
								{
									 title.setText("Ошибка: " + result);
								}
							}
				});
			}
		});
        marketPricePanel.add(changeMarketPriceButton);
        
        _itemsListBox.addChangeHandler(new ChangeHandler()
        {
            @Override
            public void onChange(ChangeEvent event)
            {
                repaint();
            }
        });
        
        craftTable = new ItemCraftTable2(_service);
        craftTable.setEditable(false);
        setWidget(0, 1, craftTable);
        
        VerticalPanel craftedItemsPanel = new VerticalPanel();
        setWidget(1, 0, craftedItemsPanel);
        craftedItemsPanel.add(new Label("Производимые предметы"));
        craftedItemsTable = new FlexTable();
        craftedItemsPanel.add(craftedItemsTable);
        //repaint();
    }
    
    public void repaint()
    {
        _service.getItemDTO(_itemsListBox.getCurrentValueCode(), 
                new AsyncCallback<ItemDTO>()
        {
            
            @Override
            public void onSuccess(ItemDTO result)
            {
                title.setText(result.getItemTitle());
                category.setText(result.getCategoryTitle());
                fixedPrice.setText(result.getFixedPrice());
                craftPrice.setText(result.getCraftPrice());
                marketPrice.setText(result.getMarketPrice());

                if (result.getCraftInfo() != null)
                {
                	craftTable.setDefaultValues(result.getCraftInfo());
                	craftTable.setVisible(true);
                }
                else
                {
                	craftTable.setVisible(false);
                }
                
                craftedItemsTable.removeAllRows();
                if (result.getCreatedItems() != null)
                {
                	for (int i = 0; i < result.getCreatedItems().size(); i++)
                		craftedItemsTable.setText(i, 0, result.getCreatedItems().get(i).getResultItem().getTitle());
                }
            }
            
            @Override
            public void onFailure(Throwable caught)
            {
                // TODO Auto-generated method stub
                title.setText("Ошибка работы с сервисом");
            }
        });
    }
}
